<!DOCTYPE html>
<html xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/static/pengpai/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">附件名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="fileName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">附件格式</label>
                    <div class="layui-input-inline">
                        <input type="text" name="fileSuff" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="attach-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="attach-table" lay-filter="attach-table"></table>
    </div>
</div>

<script type="text/html" id="attach-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" sec:authorize="hasAuthority('web:attach:create')">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" sec:authorize="hasAuthority('web:attach:delete')">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="attach-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" sec:authorize="hasAuthority('web:attach:update')"><i class="layui-icon layui-icon-edit">编辑</i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" sec:authorize="hasAuthority('web:attach:delete')"><i class="layui-icon layui-icon-delete">删除</i></button>
</script>

<script type="text/html" id="attach-uploadTime">
    {{layui.util.toDateString(d.uploadTime, 'yyyy-MM-dd HH:mm:ss')}}
</script>
<script type="text/html" id="attach-fileSize">
    {{(d.fileSize / 1024).toFixed(1) kb}}
</script>

<script src="/static/pengpai/component/layui/layui.js"></script>
<script src="/static/pengpai/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery','common', 'popup'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let popup = layui.popup;

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '附件名称',
                    field: 'fileName',
                    align: 'center',
                    width: 130
                },
                {
                    title: '附件大小',
                    field: 'fileSize',
                    align: 'center',
                    width: 130
                },
                {
                    title: '访问路径',
                    field: 'visitUrl',
                    align: 'center'
                },
                {
                    title: '附件路径',
                    field: 'filePath',
                    align: 'center',
                    width: 180
                },
                {
                    title: '附件格式',
                    field: 'fileSuff',
                    align: 'center',
                },
                {
                    title: '上传用户',
                    field: 'uploadUser',
                    align: 'center',
                    width: 100,
                },
                {
                    title: '上传时间',
                    field: 'uploadTime',
                    align: 'center',
                    templet: '#attach-uploadTime',
                },
                {
                    title: '操作',
                    toolbar: '#attach-bar',
                    align: 'center',
                    width: 300
                }
            ]
        ]

        table.render({
            elem: '#attach-table',
            url: '/webAttach/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#attach-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(attach-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'grant') {
                window.grant(obj);
            }
        });

        table.on('toolbar(attach-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(attach-query)', function(data) {
            table.reload('attach-table', {
                where: data.field
            })
            return false;
        });


        window.add = function() {
            var index = layer.open({
                title: '添加附件',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['50%', '60%'],
                content: '/webAttach/add',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }

        window.edit = function(obj) {
            var index = layer.open({
                title: '编辑用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/webAttach/edit?id='+obj.data.fileId,
                // content: mv.page,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }

        window.grant = function(obj) {
            // debugger
            var index = layer.open({
                title: '角色授权',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/webAttach/grant?id='+obj.data.fileId,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }

        window.remove = function(obj) {
            layer.confirm('确定要删除该用户', {
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/webAttach/del",
                    data: {"ids":obj.data['fileId']},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, function() {
                                obj.del();
                                table.reload('attach-table', {
                                    done: function (res, curr, count) {
                                        if (curr > 1 && res.data.length === 0) {
                                            curr = curr - 1;
                                            table.reload('attach-table', {
                                                page: {
                                                    curr: curr
                                                },
                                            })
                                        }
                                    }
                                });
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.batchRemove = function(obj) {

            var ids = common.checkField(obj,'fileId');

            if (ids === "") {
                layer.msg("未选中数据");
                return false;
            }

            layer.confirm('确定要删除这些用户', {
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/webAttach/del",
                    data: {"ids":ids},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            popup.success(result.msg, function () {
                                console.log(obj);
                                table.reload('attach-table', {
                                    done: function (res, curr, count) {
                                        if (curr > 1 && res.data.length === 0) {
                                            curr = curr - 1;
                                            table.reload('attach-table', {
                                                page: {
                                                    curr: curr
                                                },
                                            })
                                        }
                                    }
                                });
                            });
                        }else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('attach-table');
        }
    })
</script>
</body>
</html>
